package com.hanbang.taitaigou.ui.activity.base;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.hanbang.taitaigou.R;
import com.hanbang.taitaigou.ui.activity.aboutme.AboutMeActivity;
import com.hanbang.taitaigou.ui.activity.application.ActivityManager;
import com.hanbang.taitaigou.ui.activity.gouwuche.GouWuCheActivity;
import com.hanbang.taitaigou.ui.activity.home.MainActivity;
import com.hanbang.taitaigou.ui.activity.taitaigou.TaiTaiGouActivity;
import com.hanbang.taitaigou.ui.widget.CircleTextView;
import com.hanbang.taitaigou.utils.db.GwcDbUtil;
import com.hanbang.taitaigou.utils.ui.LogUtils;
import com.hanbang.taitaigou.utils.ui.ToastUtils;
import com.hanbang.taitaigou.utils.ui.UiUtils;

import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

public class MainBaseActivity extends BaseActivity {
    private long exitTime = 0;
    private int currentBotton;
    private ViewGroup currentView;
    @ViewInject(value = R.id.number)
    private CircleTextView circleTextView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityManager.getInstance().pushActivity(this);
        IntentFilter intentFilter = new IntentFilter(GwcDbUtil.GWC_CHANG_ADD);
        intentFilter.addAction(GwcDbUtil.GWC_CHANG_SUB);
        intentFilter.addAction(GwcDbUtil.GWC_CHANG);
        intentFilter.addAction(GwcDbUtil.GWC_CHANG_CLEAR);
        intentFilter.addAction(GwcDbUtil.DELETE_DB);
        registerReceiver(broadcastReceiver, intentFilter);
    }

    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);
        upDataNumber();
    }

    public void setCurrentBotton(int currentBotton) {
        this.currentBotton = currentBotton;
        ViewGroup current = null;
        switch (currentBotton) {
            case 1:
                current = (ViewGroup) findViewById(R.id.bottom_bar_ll1);
//                home.setImageResource(R.mipmap.home_1);
                break;
            case 2:
                current = (ViewGroup) findViewById(R.id.bottom_bar_ll2);
                break;
            case 3:
                current = (ViewGroup) findViewById(R.id.bottom_bar_ll3);
                break;
            case 4:
                current = (ViewGroup) findViewById(R.id.bottom_bar_ll4);
                break;

        }
        if (current != null) {
            UiUtils.setButton(current, true);
            currentView = current;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (currentView != null) {
            UiUtils.shakeUp(currentView.getChildAt(0), 1.2f, 18);
        }
    }

    /**
     * 更新购物车的数量   少用
     */
    public void upDataNumber() {
        if (circleTextView != null) {
            upDataNumber(GwcDbUtil.getGwcAllNumber());
        }
    }
    /**
     * 更新购物车的数量
     */
    public void upDataNumber(int number) {
        if (circleTextView != null) {
            circleTextView.setNUmber(number);
        }
    }
    /**
     * 更新购物车的数量 加一
     */
    public void upDataNumberAdd() {
        if (circleTextView != null) {
            circleTextView.setNUmber(circleTextView.getNumber() + 1, true);
        }
    }

    /**
     * 更新购物车的数量 减一
     */
    public void upDataNumberSub() {
        if (circleTextView != null) {
            circleTextView.setNUmber(circleTextView.getNumber() - 1, true);
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(broadcastReceiver);
    }

    @Override
    public void onBackPressed() {
        isExitApplication(this);
    }


    public void isExitApplication(Context context) {
        if ((System.currentTimeMillis() - exitTime) > 2000) {
            ToastUtils.show(this, "再按一次退出程序", Toast.LENGTH_SHORT);
            exitTime = System.currentTimeMillis();
        } else {
            // 退出
            ActivityManager.getInstance().exitAllActivity();
        }
    }


    @Event(value = R.id.bottom_bar_ll1, type = View.OnClickListener.class)
    private void bottomBar1OnClick(View view) {
        if (currentBotton != 1) {
//            home.setImageResource(R.mipmap.home_1);
            Intent intent = new Intent(this, MainActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
            startActivity(intent);
        }
    }

    @Event(value = R.id.bottom_bar_ll2, type = View.OnClickListener.class)
    private void bottomBar2OnClick(View view) {
        if (currentBotton != 2) {
            Intent intent = new Intent(this, TaiTaiGouActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
            startActivity(intent);
        }
    }

    @Event(value = R.id.bottom_bar_ll3, type = View.OnClickListener.class)
    private void bottomBar4OnClick(View view) {
        if (currentBotton != 3) {
            Intent intent = new Intent(this, AboutMeActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
            startActivity(intent);
        }
    }

    @Event(value = R.id.bottom_bar_ll4, type = View.OnClickListener.class)
    private void bottomBar5OnClick(View view) {
        if (currentBotton != 4) {
            Intent intent = new Intent(this, GouWuCheActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
            startActivity(intent);
        }
    }

    BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            LogUtils.e("sssssssss", currentBotton + action);
            if (TextUtils.equals(action, GwcDbUtil.GWC_CHANG_ADD)) {
                upDataNumberAdd();
            } else if (TextUtils.equals(action, GwcDbUtil.GWC_CHANG_SUB)) {
                upDataNumberSub();
            } else if (TextUtils.equals(action, GwcDbUtil.GWC_CHANG)) {
                upDataNumber();
            }else if (TextUtils.equals(action, GwcDbUtil.GWC_CHANG_CLEAR)) {
                upDataNumber(0);
            }else if (TextUtils.equals(action, GwcDbUtil.DELETE_DB)) {
                upDataNumber();
            }
        }
    };

}
